﻿/*
 ***************************************************************************
 * Microsoft Visual C# .NET
 * 名称：       App_Input_PersonOvertimeSrhForm
 * 类型：       控制层
 * 功能说明：   职员加班列表页面
 * 作者：       黄鑫
 * 创建日期：   2007/10/30
 * 修改日期     修改者      修改内容
 * 
 * 
 * ---
 ***************************************************************************
*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Shpm.System.Validator;
using Shpm.System.Web.Control;
using log4net;
using Shpm.App.Input.Bean;
using Shpm.App.Input.Dao;
using Shpm.App.Commons.Db.Beans.Master;
using Shpm.System;
using Shpm.App.Commons.Utils;
using Shpm.App.Master.Dao;
using Shpm.App.Master.Bean;
using Shpm.App.Common.Bean;

public partial class App_Input_PersonOvertimeSrhForm : System.Web.UI.Page
{
    // log
    protected static ILog log = LogManager.GetLogger(typeof(App_Input_PersonOvertimeSrhForm));

    private const string SELECT_ALL = "-1";
    private const string LIMIT_PERSON = "2";
    private const string LIMIT_ZERO = "0";
    private const int CELL_AUDITINGNAME = 6;

    // page load
    protected void Page_Load(object sender, EventArgs e)
    {
        txtEmpId.Attributes.Add("onblur", "showEmployeeName(this.id,'" + txtEmpName.ClientID + "');");
        txtEmpName.Attributes.Add("onfocus", "document.getElementById('" + txtEmpId.ClientID + "').focus();");
        if (!Page.IsPostBack)
        {
            // 取得当前登录的用户id信息
            SysUserManager manager = new SysUserManager(Context);
            SysUserBean sysUserBean = manager.getSysUserBean();
            MstSysUserBean mstSysUserBean =
                sysUserBean == null ? null : sysUserBean.getMstSysUserBean();
            MstEmployeeBean mstEmpBean =
                sysUserBean == null ? null : sysUserBean.getEmployeeBean();
            string loginId = mstSysUserBean == null ? "" : mstSysUserBean.SysUserId;
            this.ViewState["USER_ID"] = loginId;
            
            initPageControls();
            this.ViewState["SEARCH_FLG"] = "1";
            // 接收传递参数为分页编号
            if (Context.Items.Contains("ParamPageIndex"))
            {
                viewSrhRst.PageIndex = Convert.ToInt32(Context.Items["ParamPageIndex"]);
            }
            if (Context.Items.Contains("CMB_GROUP_ID"))
            {
                this.ViewState["GROUP_ID"] = Convert.ToString(Context.Items["CMB_GROUP_ID"]);
                lstGroup.SelectedValue = Convert.ToString(this.ViewState["GROUP_ID"]);
                if (lstGroup.SelectedValue != SELECT_ALL)
                {
                    setLstJob(Convert.ToString(this.ViewState["GROUP_ID"]),"");
                }
                this.ViewState["SEARCH_FLG"] = "1";
            }
            if (Context.Items.Contains("CMB_JOB_ID"))
            {
                this.ViewState["JOB_ID"] = Convert.ToString(Context.Items["CMB_JOB_ID"]);
                lstJob.SelectedValue = Convert.ToString(this.ViewState["JOB_ID"]);
            }
            if (Context.Items.Contains("CMB_EMPLOYEE_ID"))
            {
                this.ViewState["EMPLOYEE_ID"] = Convert.ToString(Context.Items["CMB_EMPLOYEE_ID"]);
                this.txtEmpId.Text = Convert.ToString(this.ViewState["EMPLOYEE_ID"]);
            }
            if (Context.Items.Contains("CMB_EMPLOYEE_NAME"))
            {
                this.ViewState["EMPLOYEE_NAME"] = Convert.ToString(Context.Items["CMB_EMPLOYEE_NAME"]);
                this.txtEmpName.Text = Convert.ToString(this.ViewState["EMPLOYEE_NAME"]);
            }
            if (Context.Items.Contains("CMB_BEGIN_DATE"))
            {
                this.ViewState["BEGIN_DATE"] = Convert.ToString(Context.Items["CMB_BEGIN_DATE"]);
                this.txtBeginDate.Text = Convert.ToString(this.ViewState["BEGIN_DATE"]);
            }
            if (Context.Items.Contains("CMB_END_DATE"))
            {
                this.ViewState["END_DATE"] = Convert.ToString(Context.Items["CMB_END_DATE"]);
                this.txtEndDate.Text = Convert.ToString(this.ViewState["END_DATE"]);
            }
            
            PersonEmployeeBean bean = new PersonEmployeeBean();
            PersonEmployeeDao dao = new PersonEmployeeDao();
            bean.EmployeeId = loginId;
            bean = dao.Find(bean);
            if(bean.GroupId == LIMIT_PERSON)
            {
                return;
            }
            this.ViewState["GROUP_ID"] = bean.GroupId;
            this.lstGroup.SelectedValue = bean.GroupId;
            setLstJob(bean.GroupId,Convert.ToString(bean.JobId));
            lstGroup.Enabled = false;
            if (bean.JobLimit != LIMIT_ZERO)
            {
                return;
            }
            this.lstJob.SelectedValue = Convert.ToString(bean.JobId);
            lstJob.Enabled = false;
            this.txtEmpId.Text = bean.EmployeeId;
            this.txtEmpId.Enabled = false;
            this.txtEmpName.Text = bean.EmployeeName;
        }
    }

    // bind common catalog data 
    private void initPageControls()
    {
        PersonJobDao dao = new PersonJobDao();
        PersonJobBean bean = new PersonJobBean();
        IList lstJob1 = dao.ListJob(bean);
        lstJob1.Insert(0, new PersonJobBean());
        IList lstGroup1 = dao.ListGroup(bean);
        lstGroup1.Insert(0, new PersonJobBean());

        // 职位列表
        lstJob.DataSource = lstJob1;
        lstJob.DataBind();

        // 部门列表
        lstGroup.DataSource = lstGroup1;
        lstGroup.DataBind();

        this.ViewState["EMPLOYEE_ID"] = "";
        this.ViewState["EMPLOYEE_NAME"] = "";
        this.ViewState["BEGIN_DATE"] = "";
        this.ViewState["END_DATE"] = "";
    }


    // before render
    void Page_PreRender(object sender, EventArgs e)
    {
        if ("0".Equals(Convert.ToString(this.ViewState["SEARCH_FLG"])))
        {
            return;
        }
        // get current doctor responsibility members list
        PersonOvertimeDao dao = new PersonOvertimeDao();
        IList lstRst = null;
        PersonOvertimeBean bean = new PersonOvertimeBean();
        if (! string.IsNullOrEmpty(Convert.ToString(this.ViewState["GROUP_ID"])))
        {
            bean.GroupId = Convert.ToString(this.ViewState["GROUP_ID"]);
        }
        if (! string.IsNullOrEmpty(Convert.ToString(this.ViewState["JOB_ID"])))
        {
            bean.JobId = Convert.ToInt32(this.ViewState["JOB_ID"]);
        }
        if (!string.IsNullOrEmpty(Convert.ToString(this.ViewState["EMPLOYEE_ID"])))
        {
            bean.EmployeeId = Convert.ToString(this.ViewState["EMPLOYEE_ID"]);
        }
        if (!string.IsNullOrEmpty(Convert.ToString(this.ViewState["EMPLOYEE_NAME"])))
        {
            bean.EmployeeName = Convert.ToString(this.ViewState["EMPLOYEE_NAME"]);
        }
        if (!string.IsNullOrEmpty(Convert.ToString(this.ViewState["BEGIN_DATE"])))
        {
            bean.BeginDate = DateUtils.convertToDate(Convert.ToString(this.ViewState["BEGIN_DATE"]));
        }
        if (!string.IsNullOrEmpty(Convert.ToString(this.ViewState["END_DATE"])))
        {
            bean.EndDate = DateUtils.convertToDate(Convert.ToString(this.ViewState["END_DATE"]));
        }
        lstRst = dao.LstOvertime(bean);
        if (lstRst.Count == 0 && !string.IsNullOrEmpty(Convert.ToString(this.ViewState["Serch"])))
        {
            ((Design_MasterPage)this.Master).InfoMessage =
                    HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0039").ToString();
            viewSrhRst.DataSource = lstRst;
            viewSrhRst.DataBind();
        }
        else
        {
            viewSrhRst.DataSource = lstRst;
            viewSrhRst.DataBind();
        }
        setLstJob(bean.GroupId, Convert.ToString(bean.JobId));
    }

    // page index change
    protected void viewSrhRst_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        // 设置分页编号
        viewSrhRst.PageIndex = e.NewPageIndex;
    }


    protected void viewSrhRst_OnRowEditing(object sender, GridViewEditEventArgs e)
    {
    }

    protected void viewSrhRst_OnRowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int index = Convert.ToInt32(e.RowIndex);
        // Get the Key value from the GridView control.
        DataKey key = viewSrhRst.DataKeys[index];
        int overtimeId = Convert.ToInt32(key.Value);
        PersonOvertimeBean bean = new PersonOvertimeBean();
        PersonOvertimeDao dao = new PersonOvertimeDao();
        bean.OvertimeId = overtimeId;
        bean = dao.Find(bean);
        if (bean == null)
        {
            ((Design_MasterPage)this.Master).InfoMessage =
                    HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0003").ToString();
            return;
        }
        if (bean.AuditingFlg)
        {
            ((Design_MasterPage)this.Master).InfoMessage =
                    HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "E0380").ToString();
            return;
        }
        if(bean.EmployeeId != Convert.ToString(this.ViewState["USER_ID"]))
        {
            ((Design_MasterPage)this.Master).InfoMessage =
                    HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "E0381").ToString();
            return;

        }
        dao.Delete(bean);
        ((Design_MasterPage)this.Master).InfoMessage =
                HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0003").ToString();
    }
    

    // details click
    protected void viewSrhRst_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int index = Convert.ToInt32(e.CommandArgument);
        DataKey key = viewSrhRst.DataKeys[index];
        string overtimeId = Convert.ToString(key.Value);

        if (e.CommandName == "Edit")
        {
            string url = "~/App/Input/PersonOvertime.aspx";
            
            // 传递分页编号
            Context.Items["OVERTIME_ID"] = overtimeId;
            Context.Items["CMB_JOB_ID"] = Convert.ToString(this.ViewState["JOB_ID"]);
            Context.Items["CMB_GROUP_ID"] = Convert.ToString(this.ViewState["GROUP_ID"]);
            Context.Items["CMB_EMPLOYEE_ID"] = Convert.ToString(this.ViewState["EMPLOYEE_ID"]);
            Context.Items["CMB_EMPLOYEE_NAME"] = Convert.ToString(this.ViewState["EMPLOYEE_NAME"]);
            Context.Items["CMB_BEGIN_DATE"] = Convert.ToString(this.ViewState["BEGIN_DATE"]);
            Context.Items["CMB_END_DATE"] = Convert.ToString(this.ViewState["END_DATE"]);
            Context.Items["ParamPageIndex"] = viewSrhRst.PageIndex;
            ControllerUtils.Transfer(Context, url, true);
        }
    }

    // adjust bind data
    protected void viewSrhRst_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        /*if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (Convert.ToBoolean(((PersonOvertimeBean)e.Row.DataItem).AuditingFlg))
            {
                e.Row.Cells[CELL_AUDITINGNAME].Text = "已审批";
            }
            else if (Convert.ToBoolean(((PersonOvertimeBean)e.Row.DataItem).CommitFlg))
            {
                e.Row.Cells[CELL_AUDITINGNAME].Text = "已提交";
            }
            else
            {
                e.Row.Cells[CELL_AUDITINGNAME].Text = "未提交";
            }
        } */       
    }
    protected void btnSeach_Click(object sender, EventArgs e)
    {
        ComValidateChecker vd = new ComValidateChecker();
        vd.CheckItems.Add(new CheckDate(txtBeginDate.Text, "yyyy/MM/dd", new ComMessage("E0037", "开始日期")));
        vd.CheckItems.Add(new CheckDate(txtEndDate.Text, "yyyy/MM/dd", new ComMessage("E0037", "结束日期")));
        if (!vd.DoValidator())
        {
            ((Design_MasterPage)this.Master).InfoMessage = vd.ErrorMessage;
            this.ViewState["SEARCH_FLG"] = "0";
            return;
        }
        else
        {
            this.ViewState["SEARCH_FLG"] = "1";
            this.ViewState["GROUP_ID"] = lstGroup.SelectedValue;
            this.ViewState["JOB_ID"] = lstJob.SelectedValue;
            this.ViewState["EMPLOYEE_ID"] = txtEmpId.Text;
            this.ViewState["EMPLOYEE_NAME"] = txtEmpName.Text;
            this.ViewState["BEGIN_DATE"] = txtBeginDate.Text;
            this.ViewState["END_DATE"] = txtEndDate.Text;
            this.ViewState["Serch"] = "Serch";
        }
    }
    protected void btnNew_Click(object sender, EventArgs e)
    {
        string url = "~/App/Input/PersonOvertime.aspx";

        // 传递分页编号
        Context.Items["OVERTIME_ID"] = null;
        Context.Items["CMB_JOB_ID"] = Convert.ToString(this.ViewState["JOB_ID"]);
        Context.Items["CMB_GROUP_ID"] = Convert.ToString(this.ViewState["GROUP_ID"]);
        Context.Items["CMB_EMPLOYEE_ID"] = Convert.ToString(this.ViewState["EMPLOYEE_ID"]);
        Context.Items["CMB_EMPLOYEE_NAME"] = Convert.ToString(this.ViewState["EMPLOYEE_NAME"]);
        Context.Items["CMB_BEGIN_DATE"] = Convert.ToString(this.ViewState["BEGIN_DATE"]);
        Context.Items["CMB_END_DATE"] = Convert.ToString(this.ViewState["END_DATE"]);
        Context.Items["ParamPageIndex"] = viewSrhRst.PageIndex;
        ControllerUtils.Transfer(Context, url, true);    
    }

    //配置所属部门职位列表
    protected void setLstJob(string groupId, string jobId)
    {
        PersonJobDao dao = new PersonJobDao();
        PersonJobBean bean = new PersonJobBean();
        bean.GroupId = groupId;
        IList lstJob1 = dao.ListJob(bean);
        lstJob1.Insert(0, new PersonJobBean());


        // 职位列表
        lstJob.DataSource = lstJob1;
        lstJob.DataBind();
        if (!string.IsNullOrEmpty(Convert.ToString(jobId)))
        {
            lstJob.SelectedValue = Convert.ToString(jobId);
        }
    }
}
